package com.hqyj.crmdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.crmdemo.entity.RestResult;
import com.hqyj.crmdemo.entity.User;
import com.hqyj.crmdemo.service.impl.UserServiceImpl;
import com.hqyj.crmdemo.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 何万林
 * @since 2022-10-17
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    UserServiceImpl service;

    @Autowired
    JWTUtil jwtUtil;

    /**
     * 用户登录校验
     * @param uName,uPassword
     * @return 返回校验结果
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public RestResult<User> queryUser(String uName,String uPassword){
        //判断用户名和密码是否为空
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_name",uName).eq("u_password",uPassword);
        User u = service.queryUser(queryWrapper);
        if (u != null){
            String token = jwtUtil.createToken(u);
            return RestResult.ok(token,u);
        }else{
            return RestResult.ok("false");
        }
    }


    /**
     * 查询所有客户经理
     * @return
     */
    @RequestMapping(value = "/two",method = RequestMethod.POST)
    public RestResult<User> queryTwo(){
        //判断用户名和密码是否为空
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("r_id",2);
        List<User> list = service.list(queryWrapper);
        return RestResult.ok(list);

    }


}
